﻿
Imports System.Threading





Public Class Sleeping_Barber


    Dim Customers As Integer = 0
    Dim Barbercheck As Integer = 0
    Dim accessSeats As Integer
    Dim NumberOfFreeSeats As Integer = 3
    Dim trd1 As Thread
    Dim trd2 As Thread



   





    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '
        '
        '
        '

        Button1.Visible = False
        Button2.Visible = True
        Button3.Visible = True
        Customers += 1


        ' trd2 = New Thread(AddressOf ThreadTask2)
        ' trd2.IsBackground = True
        'trd2.Start()
        ThreadTask2()

        ' trd1 = New Thread(AddressOf ThreadTask1)
        ' trd1.IsBackground = True
        ' trd1.Start()
        ' ThreadTask1()

        'Ektelesi kinisewn tou kourea ws sintonistis
        ' Dim rnd As New Random()





    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button1.Visible = True
        Me.Close()

        'trd1.Abort()
        ' trd2.Abort()
        ' Me.Refresh()

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


    End Sub

    Function SwitchToThread(ByVal Thread) As Boolean
        Thread.Suspend()


    End Function



    Public Sub ThreadTask1()

        'SwitchToThread(trd2)

        If (NumberOfFreeSeats <= 4) Then

            CustomerLabel.Text = "Barber,I need a haircut.."
            'ThreadTask1.CancelWorkerAsync()
            'Thread.Sleep(4000)


            'CustomerLabel.Text = ""
            If (NumberOfFreeSeats <= 4 And Barbercheck = 2) Then

                NumberOfFreeSeats -= 1
                CustomerLabel.Text = "I have to wait.."
                'Thread.Sleep(2000)
                ' CustomerLabel.Text = " "
            End If


            If (NumberOfFreeSeats > 4) Then
                CustomerLabel.Text = "Too much crowded.I have to go!"
            End If


        End If



        ' SwitchToThread(trd1)

    End Sub

    Public Sub ThreadTask2()

        For work As Integer = 1 To 20

            'Customer.ReportProgress(work)
            BarberLabel.Text = "Kateftase o Pelaths: " & work.ToString
            'NumberOfFreeSeats += 1

            Me.Refresh()
            Thread.Sleep(4000)


            ThreadTask1()

            'Customer_function()
            'Customer.RunWorkerAsync()

            If (Barbercheck = 0) Then
                BarberLabel.Text = "I am awake now..."
                Me.Refresh()
                Thread.Sleep(4000)
                Me.Refresh()
                NumberOfFreeSeats += 1
                Barbercheck = 1
            End If



            BarberLabel.Text = "Ready for haircut..."
            Me.Refresh()
            Thread.Sleep(4000)

            CustomerLabel.ResetText()


            BarberLabel.Text = "Starting to cut the hair of Customer" & work.ToString
            Barbercheck = 1
            NumberOfFreeSeats -= 1
            Me.Refresh()
            Thread.Sleep(8000)

            'Customer.RunWorkerAsync()

            BarberLabel.Text = "Finished the haircut"
            Barbercheck = 2
            Me.Refresh()
            Customers += 1
            Thread.Sleep(5000)
            'Customer.RunWorkerAsync()

        Next


        BarberLabel.Text = "I return to sleep..."
        Barbercheck = 0
        Me.Refresh()
        Thread.Sleep(6000)






    End Sub

    'Private Sub Customer_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

    '    If (accessSeats <= 4) Then

    '      CustomerLabel.Text = "Barber,I need a haircut.."
    '       Thread.Sleep(3000)

    '       If (NumberOfFreeSeats > 0 And Barbercheck <> 1) Then

    '            NumberOfFreeSeats -= 1
    '            CustomerLabel.Text = "I have to wait.."
    '
    '       End If


    '   Else : CustomerLabel.Text = "Too much crowded.I have to go!"

    '   End If 



    '  End Sub

    Private Sub Customer_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        ' Button1.Visible = True
        ' Customer.CancelAsync()

    End Sub


    Private Sub Customer_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Customer.ProgressChanged

        ' CustomerLabel.Text = "Pelaths" & e.ProgressPercentage

    End Sub

  
  
End Class












'http://www.comp.nus.edu.sg/~phanquyt/cs2106/implementation_barber.html
'http://aetos.it.teithe.gr/~vassik/downloads/ade/NOTE9.pdf
'http://en.wikipedia.org/wiki/Sleeping_barber_problem
'http://math.hws.edu/bridgeman/courses/331/f05/handouts/barber.html 